﻿using HarmonyLib;
using RimWorld;
using RimWorld.Planet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Verse;
using Verse.AI;
using MVCF;
using MVCF.Utilities;
using Verse.Sound;

namespace Miao.Stand
{
    public class JobDriver_Radius20EmeraldSpray : JobDriver
    {
        public List<Thing> casheEnemys;
        const float radius = 20;
        public Verb verbRanaged;

        public ThingDef projectileDef
        {
            get
            {
                return verbRanaged.verbProps.defaultProjectile;
            }
        }

        public override bool TryMakePreToilReservations(bool errorOnFailed)
        {
            return true;
        }

        public int Fast
        {
            get
            {
                return casheEnemys.Count < 10 ? casheEnemys.Count : 10;
            }
        }
        protected override IEnumerable<Toil> MakeNewToils()
        {
            int hit = 0;
            //effecter.Cleanup();

           
            Toil toil = new Toil();
            toil.initAction = RefreshThings;
            toil.tickAction = CreateEmeraldSpray;
            toil.defaultCompleteMode = ToilCompleteMode.Never;
            toil.FailOn((t) => (pawn as StandPawn).standPower.CurLevel < 0.1f);
            yield return toil;
            void RefreshThings()
            {
                verbRanaged = pawn.VerbTracker.AllVerbs.First(a => !a.IsMeleeAttack);
                var pawns = pawn.Map.listerThings.ThingsMatching(ThingRequest.ForGroup(ThingRequestGroup.Pawn));
                casheEnemys = pawns.Where((enemy) =>
                {
                    if (enemy.HostileTo(pawn) && !(enemy as Pawn).Downed)
                    {
                        return ((pawn.Position - enemy.Position).LengthHorizontal) < radius;//返回是否小于距离
                    }
                    return false;
                }).ToList();
            }
            void CreateEmeraldSpray()
            {
                if (Find.TickManager.TicksGame % 200 == 0)
                {
                    RefreshThings();
                }
                if (pawn.IsHashIntervalTick(20 - Fast))
                //if (Find.TickManager.TicksGame % (20 + Fast) == 0)
                {
                    if (casheEnemys.Count == 0) return;

                    Thing targetEnemy = casheEnemys.RandomElement();

                    var posX = Rand.Range(-8, 8);
                    var posZ = Rand.Range(-8, 8);
                    IntVec3 offsetPos = targetEnemy.Position + new IntVec3(posX, 0, posZ);
                    if (offsetPos.Standable(this.Map))
                    {
                        //实际命中组
                        ShootLine resultingLine;//结果线
                        bool shoot = verbRanaged.TryFindShootLineFromTo(offsetPos, targetEnemy, out resultingLine);
                        if (shoot)
                        {
                            Projectile hitProjectile = (Projectile)GenSpawn.Spawn(projectileDef, resultingLine.Source, pawn.Map);
                            hitProjectile.Launch(pawn, resultingLine.Dest, targetEnemy, ProjectileHitFlags.All, false);
                            StandSoundDefOf.EmeraShot.PlayOneShot(new TargetInfo(pawn.Position, pawn.Map));
                        }
                        else//气氛组
                        {
                            var ImpactPosOffset = GenRadial.RadialPattern[hit] + targetEnemy.Position;
                            Projectile projectile = (Projectile)GenSpawn.Spawn(projectileDef,offsetPos, pawn.Map);
                            //两个参数一个是预计命中，一个是预定命中
                            projectile.Launch(pawn, ImpactPosOffset, targetEnemy.Position, ProjectileHitFlags.All, false);
                            hit++;
                        }

                    }


                }
            }
        }



    }
}
